#!/bin/sh

hostfile=/root/host/foc2new.host

ALL_host=`more $hostfile  | awk '{print $2}' | awk '{{printf"%s, ",$0}}'| sed s/,//g`
echo "ALL HOST $ALL_host"
ip_ALL=( $ALL_host  )

HY_host=`more $hostfile| grep HY  | awk '{print $2}' | awk '{{printf"%s, ",$0}}'| sed s/,//g`
echo "HY HOST $HY_host"
ip_HY=( $HY_host  )

CU_host=`more $hostfile| grep CU  | awk '{print $2}' | awk '{{printf"%s, ",$0}}'| sed s/,//g`
echo "CU HOST $CU_host"
ip_CU=( $CU_host )

CT_host=`more $hostfile| grep CT  | awk '{print $2}' | awk '{{printf"%s, ",$0}}'| sed s/,//g`
echo "CT HOST $CT_host"
ip_CT=( $CT_host  )




check_shell(){

	sed -i "2c\app_port=$1"  check.sh

}

reboot_shell(){

        sed -i "4c\app_port=$1"  reboot.sh
        sed -i "2c\app_user=\"$2\""  reboot.sh
        sed -i "5c\app_run_path=\"$3\""  reboot.sh
}


canshu(){

	app_line=`grep $1 $hostfile | awk '{print $1}'`
	app_ipaddr=`echo $1 | awk -F _ '{print $1}'`
	app_user=`grep $1 $hostfile | awk '{print $3}'`
	app_port=`grep $1 $hostfile | awk '{print $4}'`
	app_run_path=`grep $1 $hostfile | awk '{print $5}'`
	app_content_path=`grep $1 $hostfile | awk '{print $6}'`
	app_today_log_path=`grep $1 $hostfile | awk '{print $7}'`
	app_history_log_path=`grep $1 $hostfile | awk '{print $8}'`

}

reboot_host(){

	host=`more $hostfile| grep $1  | awk '{print $2}' | awk '{{printf"%s, ",$0}}'| sed s/,//g`
	echo "$1 HOST $host"
	ip_host=( $host  )

	for ((k=0; k<=`expr "${#ip_host[*]}" - 1`;k++)) ;do

        	canshu ${ip_host[k]}
        	reboot_shell $app_port $app_user $app_run_path
		sleep 3
        	ssh $app_ipaddr "bash" < reboot.sh
#		echo $app_ipaddr
#		echo $app_port
#               echo $app_user
#               echo $app_run_path	
	      
	done



}



hy() {
read -p "Reboot Hang You Server,Are you sure[y/n]:" -t  10 hyc
case "$hyc"  in  
		y)
		echo "reboot hy"
		reboot_host HY
		;;
		n)
                exit 0
                ;;
                *)
                echo "input error,please run it again!"
                ;;

esac

}

ct() {
echo "reboot ct"
reboot_host CT
}

cu() {
echo "reboot cu"
reboot_host CU
}

check_all () {

echo "check all"
for ((k=0; k<=`expr "${#ip_ALL[*]}" - 1`;k++)) ;do
        
	canshu ${ip_ALL[k]}
	check_shell $app_port
	ssh $app_ipaddr "bash" < check.sh
#        echo $app_ipaddr
#        echo $app_port

done 

}

reboot() {
echo -e "which host foc2new app will you reboot \n 1):hangyou \n 2):CT \n 3):CU \n 4):ALL \n 5):quit"

read -p "Enter you choice:" -t  10 chiose

case "$chiose" in
                1)
                hy
                ;;
                2)
                ct
                ;;
		3)
		cu
		;;
		4)
		hy
		ct
		cu
		;;
                5)
                exit 0
                ;;
                *)
                echo "input error,please run it again!"
                ;;

esac

}

check () {

	check_all
}



echo -e " 1):reboot \n 2):check "

read -p "Enter you choice(input 1 or 2):" -t 10  cmd

case "$cmd" in
		1)
                reboot
                ;;
                2)
                check
                ;;
                *)
                echo "input error,please run it again!"
                ;;

esac

exit 0
